yjx.controller('supportController', ['$rootScope', '$scope','$timeout', '$fetch', 'modalService','$init','ipCookie','wxShareService', function ($rootScope, $scope,$timeout, $fetch, modalService,$init,ipCookie,wxShareService) {

    /**swiper */
    var tabApi = '/support/classs';
    var tabList, sptSwiper;
    var width = document.body.offsetWidth;
    $init.title("支持")
    if (ipCookie('token')) {
        wxShareService.onMenuShareTimeline();
        wxShareService.onMenuShareAppMessage();
    }
    function getTabList() {
        $fetch.fetch(
            tabApi
        ).then(function (data) {
            $timeout(function () {
                $scope.tabList = data;
                $scope.curTab = $scope.tabList[0];
                for (var i = 0; i < data.length; i++) {
                    //tab切换中分页相关数据的中间寄存对象
                    $scope['child_' + data[i].id] = {
                        'pageNum': 1,
                        'paging': null,
                        '_updated': null,
                        'hideShows': true,
                        'dataList': [],
                    }
                }
                resetData($scope.curTab.id);
                $timeout(function () {
                    buildSwiper();
                },300)
            })
        })
    }
    getTabList();
    function buildSwiper() {
        var count = $scope.tabList.length>4?4:$scope.tabList.length;
        tabList = new Swiper('#tabList', {
            slidesPerView: count,
        });
        sptSwiper = new Swiper('#sptSwiper', {
            direction: 'horizontal',
            // noSwiping : true,
            slider: width,
            on: {
                slideNextTransitionStart: function () {
                    tabList.slideTo(sptSwiper.activeIndex)
                    $timeout(function () {
                        $scope.curTab = $scope.tabList[sptSwiper.activeIndex];
                        resetData($scope.curTab.id);
                    })
                },
                slidePrevTransitionStart: function () {
                    tabList.slideTo(sptSwiper.activeIndex - 1)
                    $timeout(function () {
                        $scope.curTab = $scope.tabList[sptSwiper.activeIndex];
                        resetData($scope.curTab.id);
                    })
                },
            }
        })
    }

    $scope.tabSelect = function (item, index) {
        $scope.curTab = item;
        $timeout(function () {
            sptSwiper.slideTo(index, 500);
        })
        // $timeout(function(){ sptSwiper.slideTo(item.id - 1, 500);})
    }

    function resetData(id) {
        $scope.pageNum = $scope['child_' + id].pageNum;
        $scope.hideShows = $scope['child_' + id].hideShows;
        if ($scope.paging) {
            $scope.paging = $scope['child_' + id].paging
        }
        $scope._updated = $scope['child_' + id]._updated
        $scope.dataList = $scope['child_' + id].dataList;
        if ($scope.dataList.length == 0) {
            getResource();
        }
    }
    /**列表及其翻页处理 */
    $scope.temple = '<li class="cbff" ng-repeat="item in dataList" ng-click="onClick(item.id)" ui-sref="surDetail({id:item.id})" ><support-item></support-item></li>'
    var api = '/support/items'
    var params = {
        limit: 10,
        offset: 1,
    }
    //$scope.paging = {};//不要初始化paging对象会导致更新状态在一开始为nomore
    $scope.pageNum = 1;
    $scope.hideShows = true;
    //$scope._update = false;

    function getResource() {
        params.classId = $scope.curTab.id;
        $fetch.fetch(
            api,
            {
                params: params,
                cache: false,
            }

        ).then(function (data) {
            $timeout(function () {
                $scope.paging = data.page;
                $scope.dataList = data.list;
                //更新状态、数据加载控制
                $scope._updated = 'reupdated';
                $scope.hideShows = false;
                //数据寄存
                $scope['child_' + $scope.curTab.id] = {
                    'pageNum': $scope.pageNum,
                    'paging': $scope.paging,
                    '_updated': $scope._updated,
                    'hideShows': $scope.hideShows,
                    'dataList': $scope.dataList,
                }

            })
        }).catch(function (e) {
            modalService.mainMsgError(e.msg);
        })
    }

    /*翻页处理*/
    $scope.list = function () {
        params.offset = $scope.pageNum;
        params.classId = $scope.curTab.id;
        $fetch.fetch(
            api,
            {
                params: params,
                cache: false,
            }
        ).then(function (data) {
            $timeout(function () {
                $scope.paging = data.page;
                $scope.dataList = $scope.dataList.concat(data.list);
                //更新状态、数据加载控制
                $scope._updated = 'updated';
                $scope.hideShows = false;
                //数据寄存
                $scope['child_' + $scope.curTab.id] = {
                    'pageNum': $scope.pageNum,
                    'paging': $scope.paging,
                    '_updated': $scope._updated,
                    'hideShows': $scope.hideShows,
                    'dataList': $scope.dataList,
                }
            })
        }).catch(function (e) {
            modalService.mainMsgError(e.msg);
        })
    }
    $scope.nomore = function(){
        $scope['child_'+$scope.curTab.id]._updated = 'nomore';
    }


    //$scope.onClick = function(id){
    //    var length = $scope.dataList.length;
    //    for(var i= 0; i<length;i++){
    //        if($scope.dataList[i].id ==id){
    //            $scope.dataList[i].clickTimes=$scope.dataList[i].clickTimes+1 ;
    //            break;
    //        }
    //    }
    //}

}])